// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
    let message = cloud.getWXContext()
    try {
        if (event.type == 'collect_list') {
            let num = await db.collection('gods').where({
                _openid: message.OPENID,
                'collect_list._id': event.id
            }).count()

            if (num.total == 0) {
                return await db.collection('gods').where({
                    _openid: message.OPENID
                }).update({
                    data: {
                        'collect_list': _.push({
                            _id: event.id,
                            time: new Date(),
                        })
                    }
                })
            } else {
                return "repeat"
            }
        } else if (event.type == 'gwCar_list') {
            let num = await db.collection('gods').where({
                _openid: message.OPENID,
                'gwCar_list._id': event.goods._id
            }).count()

            if (num.total == 0) {
                await db.collection('usedGoods_list').doc(event.goods._id).remove()

                await db.collection('gods').where({
                    _openid: message.OPENID
                }).update({
                    data: {
                        'gwCar_list': _.push({
                            goods: event.goods,
                            time: new Date(),
                            type: "want"
                        })
                    }
                })
                await db.collection('gods').where({
                    _openid: event.goods._openid
                }).update({
                    data: {
                        'gwCar_list': _.push({
                            goods: event.goods,
                            time: new Date(),
                            type: "have"
                        })
                    }
                })

                return "ok"
            } else {
                return "repeat"
            }
        } else if (event.type == 'gwCar_del') {
            for (const num in event.list) {
                let result = await db.collection('gods').where({
                    'gwCar_list.goods._id': event.list[num].id
                }).get()
                let arr = {}
                for (const index in result.data) {
                    let gwCar_list = result.data[index].gwCar_list;
                    let newList = []

                    gwCar_list.forEach(element => {
                        if (element.goods._id == event.list[num].id) {
                            arr = element.goods
                        } else if (element.goods._id != event.list[num].id) {
                            newList.push(element)
                        }
                    });

                    await db.collection('gods').doc(result.data[index]._id).update({
                        data: {
                            gwCar_list: newList
                        }
                    })
                }
                //加入商品列表
                await db.collection('usedGoods_list').add({
                    // data 字段表示需新增的 JSON 数据
                    data: arr
                })

            }
            return "ok"
        }
    } catch (error) {
        return error

    }
}